package com.dc.project.config;

import com.dc.base.intercept.AuthenticationInterceptor;
import com.dc.common.parser.EnumScanParser;
import com.dc.common.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;

/**
* 虚拟地址映射配置
* @author Enzo
* @date 2020-7-6
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${server.port:8080}")
    private String port;
    @Value("${server.servlet.context-path:/}")
    private String contextPath;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("前端目录：" + getVuePath());
        registry.addResourceHandler("/web/**").addResourceLocations("file:" + getVuePath());
        System.out.println("文件上传目录：" + FileUtil.getUploadFileFolder());
        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + FileUtil.getUploadFileFolder());
        System.out.println("临时文件目录：" + FileUtil.getTempFolder());
        registry.addResourceHandler("/temp/**").addResourceLocations("file:" + FileUtil.getTempFolder());
        System.out.println("资源文件路径：" +FileUtil.getStaticFileFolder().getAbsolutePath().concat(File.separator));
        registry.addResourceHandler("/files/**").addResourceLocations("file:" + FileUtil.getStaticFileFolder().getAbsolutePath().concat(File.separator));
        System.out.println("接口文档：http://localhost:" + port + contextPath+"/doc.html");
        FileUtil.deleteFile(new File(FileUtil.getTempFolderTime()));
        // 扫描枚举类
        new EnumScanParser().parse("com.dc.base.dynamic.constant");
        new EnumScanParser().parse("com.dc.common.enumeration");
        new EnumScanParser().parse("com.dc.base.common.enumeration");
        new EnumScanParser().parse("com.dc.source.constant");
    }
    @Resource
    private AuthenticationInterceptor authenticationInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加身份认证拦截器，配置拦截地址
        registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**");
    }
    @Value("${dc.web-path:/}")
    private String webPath;

    private String getVuePath() {
        StringBuilder vueLoc = new StringBuilder(FileUtil.getSourceProjectPath());
        vueLoc.append(File.separator);
        vueLoc.append("dist");
        vueLoc.append(File.separator);
        if (webPath.length() > 1) {
            vueLoc = new StringBuilder(webPath);
        }
        return vueLoc.toString();
    }
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
            }
        };
    }

}
